<?php
namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\ContractVali;
use App\Model\Account\SystemType;
use App\Model\Account\Type;
use App\Model\Backstage\ContractEarnestMoney;
use App\Tool\Error;
use Illuminate\Http\Request;
/**
 * Class ContractEarnestMoneyController
 * @package App\Http\Controllers\Backstage
 * 合同诚意金
 * date:2018-8-14
 */
class ContractEarnestMoneyController extends Controller
{

    /**
     * @return \Illuminate\Http\JsonResponse
     * 返回添加页面的下拉框数据
     */
    public function dataList(){
        $use =  SystemType::storageRedis(1);
        $data = [
            'type'     => ContractEarnestMoney::$type_arr,
            'status'   => ContractEarnestMoney::$status_arr,
            'pay_type' => ContractEarnestMoney::$pay_type_arr,
            'use'      => $use
        ];
        return $this->success('ok', $data);
    }

    /**
     * 获取诚意金列表
     */
    public function listEarnest(Request $request, ContractEarnestMoney $contractEarnestMoney){
        $map = [];
        //按时间查找
        if($request->input('start_time')){
            $map[] = ['e_date', '>=', strtotime($request->input('start_time').' 00:00:00')];
        }
        if($request->input('end_time')){
            $map[] = ['e_date', '<=', strtotime($request->input('end_time').' 23:59:59')];
        }
        //按交易类型查找
        if($request->input('e_type')){
            $map[] = ['e_type',$request->input('e_type')];
        }
        //按交易状态查找
        if($request->input('e_status')){
            $map[] = ['e_status', $request->input('e_status')];
        }
        //按部门查找
        if($request->input('e_d_id')){
            $map[] = ['e_d_id', $request->input('e_d_id')];
        }
        //按员工查找
        if($request->input('e_uid')){
            $map[] = ['e_uid', $request->input('e_uid')];
        }
        //按关键词查找
        $keywords = '';
        if($request->input('keywords')){
            $keywords = $request->input('keywords');
        }
        $list = $contractEarnestMoney->lists($map, $keywords, env('PAGE_LIMIT_ADMIN'));
        if(!$list){
            return $this->error(Error::NULL_DATA);
        }
        foreach($list as $k => $v){
            $v->e_use_name        = SystemType::getTypeDetail($v->e_use_id);  //用途
            $v->e_type_name       = ContractEarnestMoney::$type_arr[$v->e_type];     //交易方式
            $v->e_status_name     = ContractEarnestMoney::$status_arr[$v->e_status]; //状态名称
            $v->e_pay_name        = ContractEarnestMoney::$pay_type_arr[$v->e_pay_type]; //支付方式
        }
        return $this->success('ok', $list);

    }
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 添加合同 - 诚意金
     */
    public function addEarnest(Request $request, ContractEarnestMoney $contractEarnestMoney){
        $params = $request->except('token');
        if(!ContractVali::validatePrice($params)){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $contractEarnestMoney->addPrice($params);
        if($res){
            return $this->success('ok', '添加成功！');
        }
        return $this->error(Error::ADD_ERROR);
    }

    /**
     * @param Request $request
     * @param ContractEarnestMoney $contractEarnestMoney
     * @return \Illuminate\Http\JsonResponse
     * 修改诚意金
     */
    public function editEarnest(Request $request, ContractEarnestMoney $contractEarnestMoney){
        $params = $request->except('token');
        if(!ContractVali::validatePrice($params) || !$request->input('e_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $contractEarnestMoney->editPrice($params);
        if($res){
            return $this->success('ok', '修改成功');
        }
        return $this->error(Error::EDIT_ERROR);
    }

    /**
     * @param Request $request
     * @param ContractEarnestMoney $contractEarnestMoney
     * @return \Illuminate\Http\JsonResponse
     * 删除诚意金
     */
    public function delEarnest(Request $request, ContractEarnestMoney $contractEarnestMoney){
        if(!$request->input('e_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $contractEarnestMoney->delPrice($request->input('e_id'));
        if($res){
            return $this->success('ok', '删除成功');
        }
        return $this->error(Error::DEL_ERROR);
    }

}